<?php
class FollowAction extends Action {	
	var $dao;
	
	function _initialize(){
		$this->dao = service('Follow');		
	}
	
	function index(){
		
	}
	//订阅分类列表
	function sort(){
		$sortList = $this->_getSortData();	

		$tab = intval($_GET['tab']);
		if(!empty($sortList[$tab])){
			$sortList[$tab]['class'] = 'on';
			$cid = $sortList[$tab]['cid'];
		}else{
			$sortList[0]['class'] = 'on';
			$cid = $sortList[0]['cid'];
		}

		$this->assign('sortList',$sortList);

		if(count($sortList!=1)){
			$result = D('AskInfo','ask')->getAskByCate($cid,$this);

			$this->assign('data',$result['data']);
			$this->assign('html',$result['html']);
			$this->assign('count',$result['count']);
		}

		$this->setTitle( L('page_head_title') );
		$this->display();
	}
	
	function bbs(){
		$sortList = $this->getBBS();	

		$tab = intval($_GET['tab']);
		if(!empty($sortList[$tab])){
			$sortList[$tab]['class'] = 'on';
			$cid = $sortList[$tab]['cid'];
		}else{
			$sortList[0]['class'] = 'on';
			$cid = $sortList[0]['cid'];
		}

		$this->assign('sortList',$sortList);

		if(count($sortList!=1)){
			$category = explode(',',$cid);
			if( $tab==0 ){
				$list['tagid'] = array( 'IN' , $category );
			}else{
				$list['tagid'] = end( $category );
			}
			
			$list['isdel'] = 0;
			$list['top'] = 0;
			$result = D('Topic','bbs')->where( $list )->order('rTime DESC')->findpage(40);
			$this->assign('data',$result['data']);
			$this->assign('html',$result['html']);
			$this->assign('count',$result['count']);
		}

		$this->setTitle( L('page_head_title') );
		$this->display();
	}
	
	protected function getBBS($add = true) {
		//获取关注数据
		$attentionData = service ( 'Follow' )->getFollowData ( $uid = $this->mid, $type = 'bbs' );
		
		if (! empty ( $attentionData )) {
			$tagDao = model ( 'Xtag' );
			foreach ( $attentionData as &$value ) {
				$cid = $value;
				$value = array ();
				if (! $add) {
					$title = explode ( ',', $tagDao->getTagName ( $cid ) );
					$value ['title'] = array_pop ( $title );
				} else {
					$value ['title'] = $tagDao->getTagName ( end( explode(',',$cid) ) );
				}
				$value ['cid'] = $cid;
				$cids[] = $cid;
			}
		}

		$all[0] = array('title'=>L('sort_all_title'),'cid'=>implode(',',$cids),'class'=>'off');
		$attentionData = array_merge($all,$attentionData);
		return $attentionData;
	}		
	
	
	private function _getSortData(){		
		$sortList = $this->dao->getFollowData();
		
		if(!empty($sortList)){
			$tagDao = model('Xtag');
			foreach ($sortList as &$value){
				$cid = $value;
				$value = array();
				$title = explode(',',$tagDao->getTagName($cid));
				$value['title'] = array_pop($title);
				$value['cid']   = $cid;
				$value['class'] = 'off';
				$cids[] = $cid;
			}
		}

		$all[0] = array('title'=>L('sort_all_title'),'cid'=>$cids,'class'=>'off');
		$sortList = array_merge($all,$sortList);
		
		return $sortList;
	}
}
?>